//
//  ZJHSetViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "ZJHSetViewController.h"
#import "FontSizeSingleTon.h"
#import "FitHeader.h"
#import "ZJHWoDeTableViewCell.h"
#define FontSize [[FontSizeSingleTon shareFontSize]array]
#import "CachesManager.h"
@interface ZJHSetViewController ()<UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *titleArray;
@property(nonatomic, strong)NSMutableArray *leftImageArray;
@property(nonatomic, strong)UISwitch *shakeSwitch;
@property(nonatomic, strong)UILabel *cachesLable;
@end

@implementation ZJHSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
}
-(void)loadView
{
    [super loadView];
    //初始化celltitle数组
    _titleArray = [NSMutableArray arrayWithObjects:@"开启摇一摇震动", @"清理缓存", nil];
    
    //初始化cell右侧图标数组
    _leftImageArray = [NSMutableArray arrayWithObjects:@"iconfont-yaoyiyao", @"iconfont-shezhiqingchuhuancun", nil];
    
    //创建界面
    [self createUI];
}

#pragma mark 创建UI
-(void)createUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20 * ht, self.view.frame.size.width, self.view.frame.size.height - 104 - 20 * ht)];
    _tableView.bounces = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UITableView alloc]init];
    [self.view addSubview:_tableView];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"userCell";
    ZJHWoDeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ZJHWoDeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.titleLable.text = _titleArray[indexPath.row];
    cell.leftImageView.image = [UIImage imageNamed:_leftImageArray[indexPath.row]];
    [cell.rightImageView removeFromSuperview];
#pragma mark 给每个cell添加控件和方法
    switch (indexPath.row) {
        
        case 0:
        {
            _shakeSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(320 * wt, 5 * ht, 30 * wt, 30 * ht)];
            _shakeSwitch.transform = CGAffineTransformMakeScale(0.8 * wt * ht, 0.8 * wt * ht);
            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"shake"]) {
                [_shakeSwitch setOn:YES];
            }
            [_shakeSwitch addTarget:self action:@selector(shakeSwitchAction) forControlEvents:UIControlEventValueChanged];
            [cell addSubview:_shakeSwitch];
        }
            break;
        case 1:
        {
            _cachesLable = [[UILabel alloc]initWithFrame:CGRectMake(305 * wt, 5 * ht, 80 * wt, 30 * ht)];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clearCachesAction)];
            _cachesLable.textAlignment = 1;
            _cachesLable.userInteractionEnabled = YES;
            [_cachesLable addGestureRecognizer:tap];
            _cachesLable.font = [UIFont systemFontOfSize:[FontSize[2] integerValue]];
            NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
            NSString *cachePath = [path stringByAppendingPathComponent:@"Caches"];
            _cachesLable.text = [CachesManager folderSizeAtPath:cachePath];
            [cell addSubview:_cachesLable];
        }
            break;
        default:
            break;
    }
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40 * ht;
}


#pragma mark 摇一摇开关方法
-(void)shakeSwitchAction
{
    if (_shakeSwitch.on) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"shake"];
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"shake"];
    }
}

#pragma mark 清除缓存方法
-(void)clearCachesAction
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"将会清除所有缓存,离线内容以及图片,是否清除" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"清除", nil];
    alert.delegate = self;
    [alert dismissWithClickedButtonIndex:0 animated:YES];
    [alert show];
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        
        UIAlertView *succeedAlert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"清除成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        
        _cachesLable.text = [CachesManager clearCaches];
        double delayInSeconds = .5;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            
            [succeedAlert show];
        });
        
        [self performSelector:@selector(dismissAlertView:) withObject:succeedAlert afterDelay:2];
    }
    
}
-(void)dismissAlertView:(UIAlertView *)alert
{
    if (alert) {
        [alert dismissWithClickedButtonIndex:0 animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
